package com.study.jyg.rxjava2study.activity;

import android.content.Intent;
import android.os.Environment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.widget.SignatureView;

import java.io.IOException;

public class SignatureWriteActivity extends BasicActivity {

    private final String FILE_NAME = "sign.png";
    private SignatureView signatureView;


    @Override
    protected boolean isWithoutUseStatusBarColor() {
        return false;
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.colorWhite;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_signature_write;
    }

    @Override
    protected void initView() {
        Toolbar toolbar = findViewById(R.id.toolBar);
        TextView tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("写签名");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());

        signatureView = findViewById(R.id.signatureView);
        signatureView.setPaintWidth(20);
        signatureView.setFilePath(Environment.getExternalStorageDirectory().getPath()+"/"+FILE_NAME);
        Button btnConfirm = findViewById(R.id.btnConfirm);
        Button btnRwrite = findViewById(R.id.btnRwrite);

        btnConfirm.setOnClickListener(v -> {
            if (signatureView.getSigstatus()) {
                try {
                    boolean isSaved = signatureView.save();
                    if (isSaved){
                        startPrescribingSuccessActivity();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(SignatureWriteActivity.this, "请先签名", Toast.LENGTH_SHORT).show();
            }
        });
        btnRwrite.setOnClickListener(v -> {
            signatureView.clear();
        });
    }

    /**
     * 签名文件
     */
    private void startPrescribingSuccessActivity(){
        Intent intent = new Intent(mContext, PrescribingSuccessActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("fileName", FILE_NAME);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}